package com.zjsd.sqeybusiness.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.zjsd.sqeybusiness.domain.CompanyImage;
import com.zjsd.sqeybusiness.dto.CompanyInfoDTO;
import com.zjsd.sqeybusiness.mapper.CompanyImageMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.CompanyInfoMapper;
import com.zjsd.sqeybusiness.domain.CompanyInfo;
import com.zjsd.sqeybusiness.service.ICompanyInfoService;

/**
 * 企业信息Service业务层处理
 *
 * @author Zheng
 * @date 2020-11-05
 */
@Service
public class CompanyInfoServiceImpl implements ICompanyInfoService
{
    @Autowired
    private CompanyInfoMapper companyInfoMapper;
    @Autowired
    private CompanyImageMapper companyImageMapper;

    /**
     * 查询企业信息
     *
     * @param companyId 企业信息ID
     * @return 企业信息
     */
    @Override
    public CompanyInfoDTO selectCompanyInfoById(Long companyId)
    {
        CompanyInfoDTO dto = new CompanyInfoDTO();
        CompanyInfo companyInfo = companyInfoMapper.selectCompanyInfoById(companyId);
        BeanUtils.copyProperties(companyInfo,dto);

        CompanyImage image = new CompanyImage();
        image.setCompanyId(companyId);
        image.setStatus("1");
        List<CompanyImage> imageList = companyImageMapper.selectCompanyImageList(image);

        dto.setImageList(imageList);
        return dto;
    }

    /**
     * 查询企业信息列表
     *
     * @param companyInfo 企业信息
     * @return 企业信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<CompanyInfo> selectCompanyInfoList(CompanyInfo companyInfo)
    {
        return companyInfoMapper.selectCompanyInfoList(companyInfo);
    }

    /**
     * 新增企业信息
     *
     * @param companyInfo 企业信息
     * @return 结果
     */
    @Override
    public void insertCompanyInfo(CompanyInfoDTO dto)
    {
        CompanyInfo companyInfo = new CompanyInfoDTO();
        BeanUtils.copyProperties(dto,companyInfo);
        companyInfo.setCreateTime(DateUtils.getNowDate());
        companyInfoMapper.insertCompanyInfo(companyInfo);

        if(null!=dto.getImageList() && dto.getImageList().size()>0){
            for(CompanyImage image : dto.getImageList()){
                image.setCompanyId(companyInfo.getCompanyId());
                image.setStatus("1");
                companyImageMapper.insertCompanyImage(image);
            }
        }

    }

    /**
     * 修改企业信息
     *
     * @param companyInfo 企业信息
     * @return 结果
     */
    @Override
    public void updateCompanyInfo(CompanyInfoDTO dto)
    {
        CompanyInfo companyInfo = new CompanyInfoDTO();
        BeanUtils.copyProperties(dto,companyInfo);
        companyInfo.setUpdateTime(DateUtils.getNowDate());
        companyInfoMapper.updateCompanyInfo(companyInfo);
        //无效image

        CompanyImage query = new CompanyImage();
        query.setCompanyId(dto.getCompanyId());
        query.setStatus("1");
        List<CompanyImage> imageList = companyImageMapper.selectCompanyImageList(query);

        if(null!=imageList && imageList.size()>0){
            for(CompanyImage companyImage : imageList){
                companyImage.setStatus("0");
                companyImage.setUpdateTime(DateUtils.getNowDate());
                companyImageMapper.updateCompanyImage(companyImage);
            }
        }

        //重新保存image
        if(null!=dto.getImageList() && dto.getImageList().size()>0){
            for(CompanyImage image : dto.getImageList()){
                image.setCompanyId(companyInfo.getCompanyId());
                image.setStatus("1");
                image.setCreateTime(DateUtils.getNowDate());
                companyImageMapper.insertCompanyImage(image);
            }
        }
    }

    /**
     * 批量删除企业信息
     *
     * @param companyIds 需要删除的企业信息ID
     * @return 结果
     */
    @Override
    public int deleteCompanyInfoByIds(Long[] companyIds)
    {
        return companyInfoMapper.deleteCompanyInfoByIds(companyIds);
    }

    /**
     * 删除企业信息信息
     *
     * @param companyId 企业信息ID
     * @return 结果
     */
    @Override
    public int deleteCompanyInfoById(Long companyId)
    {
        return companyInfoMapper.deleteCompanyInfoById(companyId);
    }

    /**
     * 查询企业信息列表
     *
     * @param companyInfo 企业信息
     * @return 企业信息
     */
    @Override
    public List<CompanyInfo> selectCompanyInfoForMobile(CompanyInfoDTO companyInfo)
    {
        return companyInfoMapper.selectCompanyInfoForMobile(companyInfo);
    }

    /**
     * 更新企业浏览量
     * @param companyId
     * @return
     */
    @Override
    public int updateCompanyInfoPageViews(Long companyId){
        return companyInfoMapper.updateCompanyInfoPageViews(companyId);
    }

    @Override
    public SysDept selectDeptInfoByUserId(Long Userid) {


        return companyInfoMapper.selectDeptInfoByUserId(Userid);
    }

    @Override
    public CompanyInfo selectCompanyInfoByDeptId(String deptId) {
        return companyInfoMapper.selectCompanyInfoByDeptId(deptId);
    }
}
